/*
 * Date         Author    Note
 * 2014.12.01   wzwz      节约
 * 2015.06.24   wzwz      节约次数范围的bug
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "economy.h"
#include "keypane.h"
#include "mh/common.h"
#include "info.h"


#define EOCNOMY_CAPTION_WIDTH   (60)
#define EOCNOMY_CAPTION_HEIGHT  (24)
#define EOCNOMY_CAPTION_INTER   (3)
#define ECONOMY_COL_WIDTH       (70) //total width / 3
#define EOCNOMY_CAPTION_LEFT    (CX_EDGE_LEFT)
#define EOCNOMY_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT - 5)


static int CheckReadyData(HWND hWnd);
/*
 *
 */
static void CreateEconomyInterface(HWND hWnd)
{
    int i = 0, j = 0;
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);

    CreateWindow (MH_LABEL_NAME,
        MW_NUM_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        EOCNOMY_CAPTION_LEFT,
        EOCNOMY_CAPTION_TOP,
        EOCNOMY_CAPTION_WIDTH,
        EOCNOMY_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        MW_CYCLE_START_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ECONOMY_COL_WIDTH + EOCNOMY_CAPTION_LEFT,
        EOCNOMY_CAPTION_TOP,
        EOCNOMY_CAPTION_WIDTH,
        EOCNOMY_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        MW_CYCLE_END_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ECONOMY_COL_WIDTH * 2 + EOCNOMY_CAPTION_LEFT,
        EOCNOMY_CAPTION_TOP,
        EOCNOMY_CAPTION_WIDTH,
        EOCNOMY_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        MW_CYCLE_NUM_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ECONOMY_COL_WIDTH * 3 + EOCNOMY_CAPTION_LEFT,
        EOCNOMY_CAPTION_TOP,
        EOCNOMY_CAPTION_WIDTH,
        EOCNOMY_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    ushort economy[10][3] = {{0}};
    GetEconomyData(g_knit_data, economy, 10);
    for(i = 0; i < 10; ++i)
    {
        char str[32] = {0};
        sprintf(str, "#%02d", i + 1);
        CreateWindow (MH_BTN_NAME,
            str,
            WS_CHILD | WS_VISIBLE | BS_FLAT,
            IDC_STATIC,
            EOCNOMY_CAPTION_LEFT,
            EOCNOMY_CAPTION_TOP + (EOCNOMY_CAPTION_HEIGHT + EOCNOMY_CAPTION_INTER) * (i + 1),
            EOCNOMY_CAPTION_WIDTH,
            EOCNOMY_CAPTION_HEIGHT,
            hWnd, MH_BTN_BKGND_BLACK | MH_BTN_TEXT_GREEN);
        for(j = 0; j < 3; ++j)
        {
            sprintf(str, "%u", economy[i][j]);
            CreateWindow(MH_EDIT_NAME,
                str,
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT1 + i * 3 + j,
                EOCNOMY_CAPTION_LEFT + (j + 1) * ECONOMY_COL_WIDTH,
                EOCNOMY_CAPTION_TOP + (EOCNOMY_CAPTION_HEIGHT + EOCNOMY_CAPTION_INTER) * (i + 1),
                EOCNOMY_CAPTION_WIDTH,
                EOCNOMY_CAPTION_HEIGHT,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, ushort nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    int i = 0, ret = 0;
    ushort cycle_id = (nID - IDC_EDIT1) % 3;
    if(cycle_id == 0)
    {
         HWND hTmp = GetDlgItem(hWnd, nID);
        const char *sc = GetWindowCaption(hTmp);
        int start = atoi(sc);
        if(start % 2 == 0 && start != 0)
        {
            AfxInfoBox(hWnd, NULL, GetString(908));
            sc = "0";
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
     else if(cycle_id == 1)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        const char *sc = GetWindowCaption(hTmp);
        int end = atoi(sc);
        if( end & 1&& end != 0)
        {
            AfxInfoBox(hWnd, NULL, GetString(911));
            sc = "0";
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
    for(i = 0; i < 10; ++i)
    {
        HWND hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i);
        const char *sc = GetWindowCaption(hTmp);
        int start = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 1);
        sc = GetWindowCaption(hTmp);
        int end = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 2);
        sc = GetWindowCaption(hTmp);
        int times = atoi(sc);
         if(start && end)
         {
             if(start > end)
             {
                AfxInfoBox(hWnd, NULL, GetString(910));
                hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 1);
                sc = "0";
                SetWindowCaption(hTmp, sc);
                InvalidateRect(hTmp, NULL, TRUE);
             }
         }
        if(!start && !end && !times)
        {
            break;
        }
        ++ret;
    }
    CheckReadyData(hWnd);
    data.economy.line = ret;
    data.economy.change = TRUE;
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static int CheckReadyData(HWND hWnd)
{
    int ret = 0;
    int i = 0;
    for(i = 0; i < 10; ++i)
    {
        HWND hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i);
        SetEditTextColor(hTmp, MH_EDIT_TEXT_GREEN);
        UpdateWindow (hTmp, TRUE);
        const char *sc = GetWindowCaption(hTmp);
        int start = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 1);
        SetEditTextColor(hTmp, MH_EDIT_TEXT_GREEN);
        UpdateWindow (hTmp, TRUE);
        sc = GetWindowCaption(hTmp);
        int end = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 2);
        SetEditTextColor(hTmp, MH_EDIT_TEXT_GREEN);
        UpdateWindow (hTmp, TRUE);
        sc = GetWindowCaption(hTmp);
        int times = atoi(sc);

        if(start && end && times)
        {
              if(start < end )
            {
                hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i);
                SetEditTextColor(hTmp, MH_EDIT_TEXT_YELLOW);
                UpdateWindow (hTmp, TRUE);
                hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 1);
                SetEditTextColor(hTmp, MH_EDIT_TEXT_YELLOW);
                UpdateWindow (hTmp, TRUE);
                hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 2);
                SetEditTextColor(hTmp, MH_EDIT_TEXT_YELLOW);
                UpdateWindow (hTmp, TRUE);

            }
        }

    }
    return ret;
}
/*
 *
 */
static int SaveEconomyData(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    int i = 0;
    ushort economy[10][3] = {{0}};
    int ret = data.economy.line;
    for(i = 0; i < ret; ++i)
    {
        HWND hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i);
        const char *sc = GetWindowCaption(hTmp);
        int start = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 1);
        sc = GetWindowCaption(hTmp);
        int end = atoi(sc);
        hTmp = GetDlgItem(hWnd, IDC_EDIT1 + 3 * i + 2);
        sc = GetWindowCaption(hTmp);
        int times = atoi(sc);
        economy[i][0] = start;
        economy[i][1] = end;
        economy[i][2] = times;
    }
    return SetAllEconomyData(g_knit_data, economy, ret);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 30)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        int min = 0;
        int max = GetTotalKnitPage(g_knit_data);
        int value = atoi(GetWindowCaption(hTmp));
        if(((nID - IDC_EDIT1) % 3) == 2)
        {
            min = 0;
            max = 0xFFFF;
        }
        CreateKeyboardWindow(hWnd, rect, min, max, value);
    }
    else if(nID == ID_ESC)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        int err =  0;
        if(data.economy.change)
        {
            int ret = SaveEconomyData(hWnd);
            char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR) + sizeof(".con")] = { 0 };
            strcat(sc, KNIT_FILE_DIR);
            strcat(sc, GetKnitFileName(g_knit_data));
            strcat(sc, ".con");
             ret = WriteConFile(sc, g_knit_data);
             if(ret)
             {
                 printf("fail to write con file: %d, err:%d\n", ret, err);
             }
            else
            {
                MH_FILE_SYNC();
            }
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 30)
        {
            OnBtnCmd(hWnd, nID);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 30)
        {
            if(((nID - IDC_EDIT1) % 3) < 2)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 30)
        {
            if((nID - IDC_EDIT1) % 3)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 + 3 && nID < IDC_EDIT1 + 30)
        {
            nID -= 3;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 27)
        {
            nID += 3;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int EconomyWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateEconomyInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateEconomyWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_ECONOMY_SET_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = EconomyWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
        CheckReadyData(hWnd);
    }
    return hWnd;
}
